# 作者：陈睿-cruise
# 2025年02月19日14时39分39秒
# 2979386061@qq.com
import multiprocessing
import os
import time

def worker():
    print(f"Worker started (PID: {os.getpid()})")
    try:
        # 模拟一个长时间运行的进程
        while True:
            print(f"Worker still running (PID: {os.getpid()})")
            time.sleep(1)  # 每秒打印一次信息
    except KeyboardInterrupt:
        print(f"Worker interrupted (PID: {os.getpid()})")

if __name__ == "__main__":
    print(f"Main process started (PID: {os.getpid()})")

    # 创建子进程
    process = multiprocessing.Process(target=worker)
    process.start()

    # 主进程等待一段时间后强制终止子进程
    time.sleep(3)
    print(f"Main process sending kill signal to worker (PID: {os.getpid()})")
    process.kill()  # 强制杀死子进程

    # 使用 join 等待子进程清理资源
    process.join()

    print(f"Main process finished (PID: {os.getpid()})")
